//
//  SAVipEditIntegralVC.m
//  saasRetail
//
//  Created by mac on 2022/3/17.
//

#import "SAVipEditIntegralVC.h"
#import "SAMemberPointsEditAPI.h"

@interface SAVipEditIntegralVC ()<HTTPAPIDelegate,UITextFieldDelegate>

@property (nonatomic, strong) NSString *memberId;
@property (nonatomic, strong) NSString *integral;
@property (nonatomic, strong) UITextField *inputTf;
@property (nonatomic, strong) SAMemberPointsEditAPI *memberPointsEditAPI;

@end

@implementation SAVipEditIntegralVC

-(instancetype)initWithMemberId:(NSString *)memberId AndIntegral:(NSString *)integral {
    if (self = [super init]) {
        self.memberId = memberId;
        self.integral = integral;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

#pragma mark - HTTPAPIDelegate
- (void)fetchData:(id)data fromServerSuccessedWithAPI:(HTTPAPI *)api {
    if (self.memberPointsEditAPI == api) {
        [Prompt popPromptViewWithMsg:@"积分修改成功" duration:2];
        !self.refreshBlock ?: self.refreshBlock();
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)fetchDataFromServerFailedWithError:(NSError *)error API:(HTTPAPI *)api {
    [Prompt popPromptViewWithMsg:@"积分修改失败" duration:2];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (string.length == 0) {
        return YES;
    }
    if (textField.text.length > 9) {
        return NO;
    }
   
    return [self validateNumber:string];
}

- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}

#pragma mark - event
-(void)clickedCompleteBtn {
    self.memberPointsEditAPI.memberId = self.memberId;
    self.memberPointsEditAPI.points = [NSString stringWithFormat:@"%0.lf",[self.inputTf.text doubleValue]];
    [self.memberPointsEditAPI sendRequestToServerWithActivityViewAndMask:GET];
}

#pragma mark - initUI
-(void)initUI {
    [self setNavigationItemBackBBIAndTitle:@"修改积分"];
    
    self.view.backgroundColor = kBgColor;
    
    UILabel *titleLab = [[UILabel alloc] initWithFrame:SARectMake(24, StatusHeight, kBaseScreenWidth - 30, 42)];
    titleLab.text = [NSString stringWithFormat:@"当前积分:%@",self.integral];
    titleLab.textColor = kBlackColor;
    titleLab.font = kFont(16);
    [self.view addSubview:titleLab];
    
    UIView *whiteView = [[UIView alloc] initWithFrame:SARectMake(0, StatusHeight + 42, kBaseScreenWidth, 42)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:whiteView];
    
    UILabel *titleLab2 = [[UILabel alloc] initWithFrame:SARectMake(24, StatusHeight + 42, 100, 42)];
    titleLab2.text = @"修改后积分";
    titleLab2.textColor = kBlackColor;
    titleLab2.font = kFont(16);
    [self.view addSubview:titleLab2];
    
    UITextField *inputTf = [[UITextField alloc] initWithFrame:SARectMake(172, 0, 170, 42)];
    inputTf.textColor = kBlackColor;
    inputTf.font = kFont(16);
    inputTf.textAlignment = NSTextAlignmentLeft;
    inputTf.placeholder = @"输入积分";
    inputTf.clearButtonMode = UITextFieldViewModeWhileEditing;
    [whiteView addSubview:inputTf];
    inputTf.delegate = self;
    inputTf.keyboardType = UIKeyboardTypeNumberPad;
    self.inputTf = inputTf;
    
    UIButton *completeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    completeBtn.frame = SARectMake(70, StatusHeight + 113, 235, 42);
    [completeBtn setTitle:@"修改" forState:UIControlStateNormal];
    [completeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    completeBtn.titleLabel.font = kFont(18);
    completeBtn.backgroundColor = kPinkColor;
    completeBtn.layer.cornerRadius = kScaleNum(3);
    completeBtn.layer.masksToBounds = YES;
    [completeBtn addTarget:self action:@selector(clickedCompleteBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:completeBtn];
}

-(SAMemberPointsEditAPI *)memberPointsEditAPI {
    if (!_memberPointsEditAPI) {
        _memberPointsEditAPI = [SAMemberPointsEditAPI apiWithDelegate:self];
    }
    return _memberPointsEditAPI;
}

@end
